﻿using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Update
{
    public partial class Form2 : Form
    {
        private MySqlConnection myconn =
              new MySqlConnection("Host = localhost; Database = test; Username = root; Password = root");

        private Form1 another;
        public Form2()
        {
            InitializeComponent();
            add_head();
        }
        private void add_head()
        {
            this.listView1.Columns.Add("邮件编号", 60, HorizontalAlignment.Center);
            this.listView1.Columns.Add("收件人", 50, HorizontalAlignment.Center);
            this.listView1.Columns.Add("收件地址", 120, HorizontalAlignment.Center);
            this.listView1.Columns.Add("收件人联系方式", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("发件人", 60, HorizontalAlignment.Center);
            this.listView1.Columns.Add("发件地址", 120, HorizontalAlignment.Center);
            this.listView1.Columns.Add("发件人联系方式", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("到件时间", 80, HorizontalAlignment.Center);
            this.listView1.Columns.Add("邮件状态", 80, HorizontalAlignment.Center);
        }
        //按收人查找
        private void button1_Click(object sender, EventArgs e)
        {
            string recName = textBox1.Text;
            string recPhone = textBox2.Text;
            if (recPhone.Equals("") || recName.Equals(""))
            {
                MessageBox.Show(@"请将收件人和收件人联系方式填写完整", @"提醒",MessageBoxButtons.OK);
                return;
            }
            string sql = "select * from mail where receive_name='" + recName + "' and receive_tel='" + recPhone+"'";
            load(sql);
        }
        //按寄件人查找
        private void button2_Click(object sender, EventArgs e)
        {
            string postName = textBox3.Text;
            string postPhone = textBox4.Text;
            if (postPhone.Equals("") || postName.Equals(""))
            {
                MessageBox.Show(@"请将寄件人和寄件人联系方式填写完整", @"提醒", MessageBoxButtons.OK);
                return;
            }
            string sql = "select * from mail where post_name='" + postName + "' and post_tel='" + postPhone + "'";
            load(sql);
        }

        //按邮件编号查找
        private void button3_Click(object sender, EventArgs e)
        {
            string mailNo = textBox5.Text;
            if (mailNo.Equals("") )
            {
                MessageBox.Show(@"请将邮件编号填写完整", @"提醒", MessageBoxButtons.OK);
                return;
            }
            string sql = "select * from mail where mail_id=" + mailNo;
            load(sql);
        }


        private void load(String sql)
        {
            listView1.Items.Clear();
            this.listView1.BeginUpdate();
            myconn.Open();
            MySqlCommand mycom = myconn.CreateCommand();
            mycom.CommandText = sql;
            mycom.CommandType = CommandType.Text;
            MySqlDataReader sdr = mycom.ExecuteReader();
            int index = 0;
            while (sdr.Read())
            {                
                listView1.Items.Add(sdr["mail_id"].ToString());
                listView1.Items[index].SubItems.Add(sdr["receive_name"].ToString());
                listView1.Items[index].SubItems.Add(sdr["receive_address"].ToString());
                listView1.Items[index].SubItems.Add(sdr["receive_tel"].ToString());
                listView1.Items[index].SubItems.Add(sdr["post_name"].ToString());
                listView1.Items[index].SubItems.Add(sdr["post_address"].ToString());
                listView1.Items[index].SubItems.Add(sdr["post_tel"].ToString());
                listView1.Items[index].SubItems.Add(sdr["arrive_time"].ToString());
                string str;
                switch (sdr["mail_state"].ToString())
                {
                    case "0":
                        str = "未领取";
                        break;
                    case "1":
                        str = "已签收";
                        break;
                    case "2":
                        str = "已退回";
                        break;
                    default:
                        str = "已退回";
                        break;
                }
                listView1.Items[index].SubItems.Add(str);
                ++index;
                //MessageBox.Show(sdr["mail_id"].ToString(), @"Get");
            }
            myconn.Close();
            this.listView1.EndUpdate();

            if (index == 0)
            {
                MessageBox.Show(@"查无记录", @"提示");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            another=new Form1();
            this.Hide();
            another.Show();
        }
    }

}
